# Различные пути
SHARED_SRC=../sysshare/src
SHARED_BIN=../sysshare/bin
SRC=src
BIN=bin

JAVA_MAIL_PATHS=$(SHARED_BIN)/javamail-1.4.2/mail.jar:$(SHARED_BIN)/javamail-1.4.2/lib/smtp.jar:$(SHARED_BIN)/javamail-1.4.2/lib/pop3.jar:$(SHARED_BIN)/javamail-1.4.2/lib/mailapi.jar:$(SHARED_BIN)/javamail-1.4.2/lib/mail.jar
MYSQL_CONNECTOR_PATH=$(SHARED_BIN)/mysql-connector/mysql-connector-java-5.1.7-bin.jar
JUNIT_PATH=$(SHARED_BIN)/junit/junit4.6/junit-4.6.jar

# Ява компилятор и его параметры
JAVAC = javac
JAVAC_PARAMS = -classpath $(SRC):$(JAVA_MAIL_PATHS):$(MYSQL_CONNECTOR_PATH):$(JUNIT_PATH) -d $(BIN)

# Виртуальная машина Ява и её параметры
JAVA=java
JAVA_PARAMS=-classpath .:../$(JAVA_MAIL_PATHS):../$(MYSQL_CONNECTOR_PATH):../$(JUNIT_PATH)

all: compileJAVA

# Генерация кода из общего кода и из кода отдельной системы
$(BIN)/sys/timer/%.class: $(SHARED_SRC)/sys/timer/%.java 
	cp $< $(SRC)/sys/timer/ && $(JAVAC) $(JAVAC_PARAMS) $<

$(BIN)/sys/logic/%.class: $(SHARED_SRC)/sys/logic/%.java 
	cp $< $(SRC)/sys/logic/ && $(JAVAC) $(JAVAC_PARAMS) $<

$(BIN)/sys/transport/%.class: $(SHARED_SRC)/sys/transport/%.java 
	cp $< $(SRC)/sys/transport/ && $(JAVAC) $(JAVAC_PARAMS) $<

$(BIN)/sys/message/%.class: $(SHARED_SRC)/sys/message/%.java 
	cp $< $(SRC)/sys/message/ && $(JAVAC) $(JAVAC_PARAMS) $<

$(BIN)/sys/database/DBWrapper.class: $(SHARED_SRC)/sys/database/DBWrapper.java
	cp $< $(SRC)/sys/database/ && $(JAVAC) $(JAVAC_PARAMS) $<

$(BIN)/sys/utils/%.class: $(SHARED_SRC)/sys/utils/%.java 
	cp $< $(SRC)/sys/utils/ && $(JAVAC) $(JAVAC_PARAMS) $<

$(BIN)/%.class: $(SRC)/%.java 
	$(JAVAC) $(JAVAC_PARAMS) $<

# Все java-файлы
compileJAVA: $(addprefix $(BIN)/sys/utils/, ReadWriteTextFile.class) $(addprefix $(BIN)/sys/message/, UniMessage.class JSONMessage.class) $(addprefix $(BIN)/sys/transport/, MessageReceiver.class MessageReceiverPOP3.class MessageSender.class MessageSenderSMTP.class) $(addprefix $(BIN)/sys/timer/,TimerClient.class Timer.class) $(addprefix $(BIN)/sys/database/, DBWrapper.class SysaDatabase.class) $(addprefix $(BIN)/sys/logic/,MessageCheckerClient.class MessageChecker.class ) $(addprefix $(BIN)/sys/, Main.class) $(addprefix $(BIN)/sys/tests/, RunTests.class Test1.class AllSystemTest.class)

run: all
	cd $(BIN) && $(JAVA) $(JAVA_PARAMS) sys.Main 

runTests: all
	cd $(BIN) && $(JAVA) $(JAVA_PARAMS) org.junit.runner.JUnitCore sys.tests.AllSystemTest > sys/tests/result.test
